
package com.insigma.business.qrcode.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.insigma.business.qrcode.common.dto.BaseSearchRequest;
import com.insigma.business.qrcode.common.dto.ErrorCodeEnum;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import org.apache.commons.lang3.StringUtils;


public class ResponseUtil {

    
    public static <DTO> BaseResponse<DTO> setupSuccessResponse(final BaseResponse<DTO> baseResponse, final DTO resultObject){
        baseResponse.setSuccess(true);
        baseResponse.setDto(resultObject);

        return baseResponse;
    }

    
    public static <DTO> BasePagingResponse<DTO> setupSuccessPagingResponse(final BasePagingResponse<DTO> basePagingResponse ,
                                                                           final DTO resultObject,
                                                                           final BaseSearchRequest searchRequest){
        basePagingResponse.setPageNo(searchRequest.getPageNo());
        basePagingResponse.setPageSize(searchRequest.getPageSize());
        basePagingResponse.setSuccess(true);
        basePagingResponse.setDto(resultObject);

        return basePagingResponse;
    }

    
    public static <DTO> BasePagingResponse<DTO> setupSuccessPagingResponse(final BasePagingResponse<DTO> basePagingResponse ,
                                                                           final DTO resultObject,
                                                                           final BaseSearchRequest searchRequest,
                                                                           final long totalCount){
        basePagingResponse.setPageNo(searchRequest.getPageNo());
        basePagingResponse.setPageSize(searchRequest.getPageSize());
        basePagingResponse.setSuccess(true);
        basePagingResponse.setDto(resultObject);
        basePagingResponse.setTotalCount(totalCount);

        return basePagingResponse;
    }

    
    public static BaseResponse setupFailedResponse(final BaseResponse baseResponse,
                                                   final ErrorCodeEnum errorCodeEnum){
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(errorCodeEnum.getCode(),errorCodeEnum.getDesc()));

        return baseResponse;
    }

    
    public static BaseResponse setupFailedResponse(final BaseResponse baseResponse,
                                                   final ErrorCodeEnum errorCodeEnum,
                                                   final String customDesc){
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(errorCodeEnum.getCode(), StringUtils.isNotBlank(customDesc)?customDesc:errorCodeEnum.getDesc()));

        return baseResponse;
    }

    
    public static BaseResponse setupFailedResponse(final BaseResponse baseResponse,
                                                   final ScenarioCodeEnum scenarioCodeEnum,
                                                   final ErrorCodeEnum errorCodeEnum,
                                                   final String customDesc){
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(CommonConstant.PRODUCTION_APP_CODE_BFF_MIX, scenarioCodeEnum.getCode(), errorCodeEnum.getCode(), StringUtils.isNotBlank(customDesc)?customDesc:errorCodeEnum.getDesc()));

        return baseResponse;
    }

    
    public static void setupPagingResponse(final BasePagingResponse<JSONArray> basePagingResponse,
                                                   final BasePagingResponse remoteBasePagingResponse,
                                                   final JSONArray jsonArray){
        basePagingResponse.setSuccess(remoteBasePagingResponse.isSuccess());
        basePagingResponse.setErrorCtx(remoteBasePagingResponse.getErrorCtx());
        basePagingResponse.setDto(jsonArray);
        basePagingResponse.setPageNo(remoteBasePagingResponse.getPageNo());
        basePagingResponse.setPageSize(remoteBasePagingResponse.getPageSize());
        basePagingResponse.setTotalCount(remoteBasePagingResponse.getTotalCount());
    }

    
    public static void setupResponseOfJSONObj(final BaseResponse<JSONObject> baseResponse,
                                              final BaseResponse remoteBaseResponse,
                                              final JSONObject jsonObject){
        baseResponse.setSuccess(remoteBaseResponse.isSuccess());
        baseResponse.setErrorCtx(remoteBaseResponse.getErrorCtx());
        baseResponse.setDto(jsonObject);
    }

    public static <DTO> BasePagingResponse<DTO> setupSuccessPagingResponse(final DTO resultObject,
                                                                           final Integer pageNo,
                                                                           final Integer pageSize){
        BasePagingResponse basePagingResponse = new BasePagingResponse();
        basePagingResponse.setPageNo(pageNo);
        basePagingResponse.setPageSize(pageSize);
        basePagingResponse.setSuccess(true);
        basePagingResponse.setDto(resultObject);

        return basePagingResponse;
    }

    public static <DTO> BaseResponse<DTO> buildSuccessResponse(DTO resultObject){
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setSuccess(true);
        baseResponse.setDto(resultObject);

        return baseResponse;
    }

    public static <DTO> BaseResponse<DTO> buildFailResponse(DTO resultObject){
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setSuccess(false);
        baseResponse.setDto(resultObject);

        return baseResponse;
    }

    
    public static BaseResponse buildFailResponse(final ErrorCodeEnum errorCodeEnum){
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(CommonConstant.PRODUCTION_APP_CODE_BFF_MIX, errorCodeEnum.getCode(), errorCodeEnum.getCode(), errorCodeEnum.getDesc()));
        return baseResponse;
    }

    
    public static BaseResponse buildFailResponse(final ErrorCodeEnum errorCodeEnum,
                                                   String customDesc){
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(CommonConstant.PRODUCTION_APP_CODE_BFF_MIX, errorCodeEnum.getCode(), errorCodeEnum.getCode(), StringUtils.isNotBlank(customDesc)?customDesc:errorCodeEnum.getDesc()));
        return baseResponse;
    }

    
    @SuppressWarnings("unchecked")
    public static void setupResponseOfLocalDTO(final BaseResponse baseResponse,
                                               final BaseResponse remoteBaseResponse,
                                               final Object resultDto){
        baseResponse.setSuccess(remoteBaseResponse.isSuccess());
        baseResponse.setErrorCtx(remoteBaseResponse.getErrorCtx());
        baseResponse.setDto(resultDto);
    }

    public static BaseResponse buildFailResponse(final String errorCode, String desc){
        BaseResponse baseResponse =new BaseResponse();
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(errorCode, desc));

        return baseResponse;
    }

}
